package me.zhengjie.utils;


import com.alibaba.fastjson.JSON;
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.tea.*;
import com.aliyun.teautil.models.RuntimeOptions;
import me.zhengjie.utils.bo.MllSms;


/**
 * Description:     TODO
 * Copyright:	   Copyright (c)
 * Company:		   数禾理财事业部
 * Author:         caijiaolong
 * Version:        1.0
 * CreateDate:     2020/12/237:00 PM
 * <p>
 * 修改历史:
 * 日期    作者    版本  修改描述
 * ------------------------------------------------------------------
 */
public class SmsAliUtils {

    /**
     * 使用AK&SK初始化账号Client
     * @param accessKeyId
     * @param accessKeySecret
     * @return Client
     * @throws Exception
     */
    public static com.aliyun.dysmsapi20170525.Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
        com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
                // 您的 AccessKey ID
                .setAccessKeyId(accessKeyId)
                // 您的 AccessKey Secret
                .setAccessKeySecret(accessKeySecret);
        // 访问的域名
        config.endpoint = "dysmsapi.aliyuncs.com";
        return new com.aliyun.dysmsapi20170525.Client(config);
    }
    public static MllSms sendAlert(String templateID, String phoneNumbers, String code, String uid){

        // 初始化 Client，采用 AK 鉴权访问的方式，此方式可能会涉及安全风险，更安全的鉴权访问方式请参考各语言文档，例如 Java：https://help.aliyun.com/document_detail/378657.html
        com.aliyun.dysmsapi20170525.Client client = null;
        try {
            client = SmsAliUtils.createClient("LTAI5tL3j9WFnb6zXmDrR7Wt", "zV4hCjUFG2AozZn7ShQV97OHdg9XqU");
        } catch (Exception e) {
            e.printStackTrace();
        }
        com.aliyun.dysmsapi20170525.models.SendSmsRequest sendSmsRequest = new com.aliyun.dysmsapi20170525.models.SendSmsRequest()
                .setTemplateCode("SMS_262465567")
                .setTemplateParam("{\"code\":\""+code+"\"}")
                .setPhoneNumbers(phoneNumbers)
                .setSignName("上海走洪文化传媒");
        MllSms mllSms =new MllSms();

        try {
            // 复制代码运行请自行打印 API 的返回值
            SendSmsResponse sendSmsResponse = client.sendSmsWithOptions(sendSmsRequest, new RuntimeOptions());
            System.out.println(JSON.toJSONString(sendSmsResponse));
            mllSms.setRequestId(sendSmsResponse.getBody().getRequestId());
            mllSms.setResponse(sendSmsResponse.getBody().getMessage());
            mllSms.setSdkAppId(sendSmsResponse.getBody().getCode());
        } catch (TeaException error) {
            // 如有需要，请打印 error
            com.aliyun.teautil.Common.assertAsString(error.message);
        } catch (Exception _error) {
            TeaException error = new TeaException(_error.getMessage(), _error);
            // 如有需要，请打印 error
            com.aliyun.teautil.Common.assertAsString(error.message);
        }



        mllSms.setEndpoint("sms.aliyuncs.com");
        mllSms.setOperator(uid);
        mllSms.setPhones(JSON.toJSONString(phoneNumbers));

        mllSms.setTemplateId(templateID);
        mllSms.setTemplateParams(code);

        return mllSms;


    }
    public static void main(String[] args_) throws Exception {
        int code = (int) ((Math.random() * 9 + 1) * 100000);

        MllSms mllSms = SmsAliUtils.sendAlert("1135171", "17191760521", code+"", "ds");

    }


}
